# -*- coding: utf-8 -*-
# @Time    : 2022/7/16 14:50
# @Author  : vvh
# @Email   : 2647167477@qq.com
# @File    : goods_model_page.py
# @Software: PyCharm
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from pages.base_pageage import BasePage

class GoodsModelPage(BasePage):
    """产品分类页"""
    # 定位器
    add_model_btn_locator = (By.ID, 'add_category') # 添加产品类别定位器

    # 添加页定位器
    model_name_locator = (By.ID, 'name')
    parent_class_locator = (By.ID, 'categoryList')
    describe_locator = (By.CSS_SELECTOR, '#dialog-message1 > form > div:nth-child(3) > div > textarea')
    submit_btn_loctor = (By.CSS_SELECTOR, '#dialog-message1 > form > div:nth-child(4) > div > input:nth-child(1)')

    # 编辑页定位器
    edit_model_name_locator = (By.CSS_SELECTOR,'#name')
    edit_parent_class_locator = (By.CSS_SELECTOR,'#categoryList')
    edit_describe_locator = (By.CSS_SELECTOR, '#dialog-message2 > form > div:nth-child(4) > div > textarea')
    edit_submit_btn_loctor = (By.CSS_SELECTOR, '#dialog-message2 > form > div:nth-child(5) > div > input:nth-child(1)')
    close_edit_btn_locator = (By.CSS_SELECTOR,'body > div:nth-child(9) > div.ui-dialog-titlebar.ui-widget-header.ui-corner-all.ui-helper-clearfix > button')

    # 清理环境定位器
    tbody_locator = (By.TAG_NAME, 'tbody')
    tr_locator = (By.TAG_NAME, 'tr')
    td_locator = (By.TAG_NAME, 'td')
    check_box_locator = (By.TAG_NAME, 'input')
    del_btn_locator = (By.CSS_SELECTOR, 'body > div.container > div.row > form > div:nth-child(1) > div > input')

    def switch_add_model(self):
        """点击进入添加产品分类页"""
        self.model_page = BasePage(self.driver)
        self.model_page.find(self.add_model_btn_locator).click()
    def switch_edit_model(self,edit_model):
        """点击进入编辑产品分类页"""
        model_lst = self.driver.find_element(*self.tbody_locator).find_elements(*self.tr_locator)
        for row in model_lst:
            # 找到需要修改的分类名所在行
            add_model = row.find_elements(*self.td_locator)[1].text
            if edit_model == add_model or ('--' + edit_model) == add_model:
                # 进入编辑分类名页面
                row.find_elements(*self.td_locator)[5].find_element(By.LINK_TEXT, '编辑').click()
                break
    # 添加产品分类页方法
    def input_model_name(self,model_name):
        """输入添加分类名"""
        self.model_page.find(self.model_name_locator).send_keys(model_name)

    def select_parent_class(self,parent_class):
        """选择父类的值"""
        self.parent_class = self.model_page.find(self.parent_class_locator)
        Select(self.parent_class).select_by_visible_text(parent_class)

    def input_describe(self,describe):
        """输入添加分类描述"""
        self.model_page.find(self.describe_locator).send_keys(describe)


    def submit_model(self):
        """点击提交"""
        self.model_page.find(self.submit_btn_loctor).click()


    def del_model_name(self,model_name):
        """删除产品分类"""
        model_lst = self.driver.find_element(*self.tbody_locator).find_elements(*self.tr_locator)
        for row in model_lst:
            # 找到新建的分类名所在行
            add_model = row.find_elements(*self.td_locator)[1]
            if model_name == add_model.text or ('--'+ model_name) == add_model.text:
                row.find_elements(*self.td_locator)[0].find_element(By.TAG_NAME, 'input').click()
                self.driver.find_element(*self.del_btn_locator).click()

    def tick_check_box(self,model_name):
        """勾选需要删除的分类名"""
        model_lst = self.driver.find_element(*self.tbody_locator).find_elements(*self.tr_locator)
        for row in model_lst:
            # 找到新建的分类名所在行
            add_model = row.find_elements(*self.td_locator)[1]
            if model_name == add_model.text or ('--' + model_name) == add_model.text:
                row.find_elements(*self.td_locator)[0].find_element(By.TAG_NAME, 'input').click()

    def cick_del_btn(self):
        """点击删除按钮"""
        self.driver.find_element(*self.del_btn_locator).click()

    def get_every_model(self):
        """获取所有的分类名组成字符串返回"""
        model_lst = self.driver.find_element(*self.tbody_locator).find_elements(*self.tr_locator)
        str_model = ''
        for row in model_lst:
            # 找到新建的分类名所在行
            add_model = row.find_elements(*self.td_locator)[1].text
            str_model += add_model
        return str_model

    def get_every_model_descripe(self,model_name):
        """获取分类名的描述"""
        model_lst = self.driver.find_element(*self.tbody_locator).find_elements(*self.tr_locator)
        for row in model_lst:
            # 找到分类名所在行
            edit_model = row.find_elements(*self.td_locator)[1].text
            if model_name == edit_model or '--' + model_name == edit_model:
                descripe = row.find_elements(*self.td_locator)[4].text
                return descripe

    # 编辑产品分类页方法
    def input_edit_model_name(self,model_name):
        """输入编辑分类名"""
        self.driver.find_element(*self.edit_model_name_locator).send_keys(model_name)

    def clear_model_name(self):
        """清除编辑分类名"""
        self.driver.find_element(*self.edit_model_name_locator).clear()

    def get_model_name(self):
        """获取编辑分类名"""
        return self.driver.find_element(*self.edit_model_name_locator).get_attribute("value")

    def select_edit_parent_class(self,parent_class):
        """选择父类的值"""
        self.parent_class = self.driver.find_element(*self.edit_parent_class_locator)
        Select(self.parent_class).select_by_visible_text(parent_class)

    def get_parent_class(self):
        """获取编辑父类的值"""
        parent_class = self.driver.find_element(*self.edit_parent_class_locator)
        return Select(parent_class).first_selected_option.text

    def input_edit_describe(self,describe):
        """输入编辑描述"""
        self.driver.find_element(*self.edit_describe_locator).send_keys(describe)

    def get_describe(self):
        """获取编辑描述的值"""
        return self.driver.find_element(*self.edit_describe_locator).text

    def clear_describe(self):
        """清除编辑描述"""
        self.driver.find_element(*self.edit_describe_locator).clear()

    def submit_edit_model(self):
        """点击提交编辑"""
        self.driver.find_element(*self.edit_submit_btn_loctor).click()

    def click_close_btn(self):
        """带你就取消编辑"""
        self.driver.find_element(*self.close_edit_btn_locator).click()